#include <stdio.h>
#include <stdlib.h>

/********************************************************************************
 * Problema da disciplina de Estrutura de Dados e Complexidade de Algoritmos da *
 * Pos Graduacao do Periodo 2011.1                                              *
 *                                                                              *
 * @see http://br.spoj.pl/problems/TV/                                          *
 * @author Elenilson Vieira - 111100371 - elenilson[at]elenilsonvieira.com      *
 * @since 16 de maio de 2011                                                    *
 ********************************************************************************/
int main3(int argc, char** argv) {

    int test = 1;
    int rowCount = 0;
    int columnCount = 0;

    while(scanf("%d %d", &rowCount, &columnCount) && rowCount != 0 || columnCount != 0){
        int array[rowCount][columnCount];

        /*** Preenche a matriz ***/
        int row = 0;
        int column = 0;
        for(row = 0; row < rowCount; row++)
            for(column = 0; column < columnCount; column++)
                scanf("%d", &array[row][column]);

        int columnArrayBeginIndex = 0;
        int rowArrayBeginIndex = 0;

        /*** Pega sequencia de comandos e executa ***/
        int x = 0;
        int y = 0;
        while(scanf("%d %d", &x, &y) && x != 0 || y != 0){
            /*** y e' invertido por que o plano de y cresce pra baixo ***/
            y = -y;
            
            if (x > 0)
               x = (x % columnCount) - columnCount;

            if (y > 0)
               y = (y % rowCount) - rowCount;

            columnArrayBeginIndex = (columnArrayBeginIndex - x) % columnCount;
            rowArrayBeginIndex = (rowArrayBeginIndex - y) % rowCount;
        }

        /*** Imprime a matriz ***/
        int j = 0;
        int i = 0;
        printf("Teste %d\n", test++);

        for (row = rowArrayBeginIndex, j = 0; j < rowCount; row++, j++) {

            column = columnArrayBeginIndex;
            printf("%d", array[row % rowCount][column % columnCount]);
            for (column = columnArrayBeginIndex + 1, i = 1; i < columnCount; column++, i++)
                printf(" %d", array[row % rowCount][column % columnCount]) ;

            printf("\n");
        }
        
        printf("\n");
    }

    return (EXIT_SUCCESS);
}

